/**
 * Created by Tink Cai on 7/31/2017.
 */
angular.module('tinkcai.shiai', []).controller('examConfigController', function ($scope, $http) {
    var getOutput = function (input) {
        var output = angular.copy(input);
        output.forEach(function (titleModel) {
            delete titleModel.isDisplay;
        });
        return output;
    };

    var findMaxIndex = function () {
        var divs = document.getElementsByTagName("div");
        for (var i = 0, max = 0; i < divs.length; i++) {
            max = Math.max(max, divs[i].style.zIndex || 0);
        }
        return max;
    }

    var cache = localStorage.memoryList;
    if (!cache) {
        $scope.memoryList = [];
    }
    else {
        $scope.memoryList = JSON.parse(cache);
    }

    $http({
        method: 'GET', url: '/shiai/shiai.json?' + new Date().getTime()
    }).then(function successCallback(response) {
        $scope.shiai = angular.copy(response.data);
    }, function errorCallback(response) {
        $scope.shiai = angular.copy([]);
    });

    $scope.toggleTitles = function (titleModel) {
        titleModel.isDisplay = !titleModel.isDisplay;
    };

    $scope.showExam = function (examModel) {
        $scope.examModel = examModel;
    };

    $scope.addTitle = function () {
        $scope.shiai.push({
            title: '', children: [{
                exam: [{}]
            }]
        });
    };

    $scope.addSubTitle = function (titleModel) {
        titleModel.children.push({});
    };

    $scope.removeBigTitle = function (index, shiaiList) {
        shiaiList.splice(index, 1);
    };

    $scope.addItem = function (examItem) {
        if (!examItem.options) {
            examItem.options = [];
        }
        examItem.options.push({});
    };

    $scope.removeItem = function (index, list) {
        list.splice(index, 1);
    };

    $scope.removeExam = function (index, list) {
        list.splice(index, 1);
    };

    $scope.removeSubTitle = function (index, list) {
        list.splice(index, 1)
    };

    $scope.addExam = function (examModel) {
        if (!examModel.exam) {
            examModel.exam = [];
        }
        examModel.exam.push({});
    };

    $scope.save = function () {
        $http({
            method: 'POST', url: '/shiai/save', data: getOutput($scope.shiai)
        }).then(function successCallback(response) {
            console.info(JSON.stringify($scope.shiai));
            alert('保存成功');
        }, function errorCallback(response) {
            // 请求失败执行代码
            alert('保存失败');
        });
    };

    $scope.up = function (index, list) {
        if (index > 0) {
            var temp = list.splice(index, 1);
            list.splice(index - 1, 0, temp[0]);
        }
    };

    $scope.down = function (index, list) {
        if (index < list.length - 1) {
            var temp = list.splice(index, 1);
            list.splice(index + 1, 0, temp[0]);
        }
    };

    $scope.copyModel = function () {
        var newData;
        try {
            newData = JSON.parse($scope.inputCode);
            $('#inputModel').modal('hide');
            $scope.shiai = newData;
            delete $scope.examModel;
            alert('替换题目成功');
        }
        catch (e) {
            alert('输入代码格式有误');
        }
    };


    $scope.$watch('memoryList', function () {
        localStorage.memoryList = JSON.stringify($scope.memoryList);
    }, true);

    $scope.setCache = function () {
        $scope.memoryList.push($scope.confirmInput);
        localStorage[$scope.confirmInput] = JSON.stringify($scope.shiai);
        $('#confirm').modal('hide');
        alert('备份成功');
    };

    $scope.changeCache = function (selectedCache) {
        $scope.inputCode = localStorage[selectedCache];
    };
});